﻿@model TestOnlineProject.Models.Entities.QuestionModel
@using TestOnlineProject.Common
@using TestOnlineProject.Models.Entities
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
    function check(c, i) {
        $("." + c).attr('checked', false);
        $("#" + i).attr('checked', true);
    }

    tinymce.init({
        menubar: false,
        toolbar: "bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent",
        resize: false,
        statusbar: true,
        height: 150,
        selector: "textarea.mceEditor"
    });

</script>
<div class="site_map">
    @Html.ActionLink("Home", "Index", "Home") / @Html.ActionLink("Subjects", "Index", "Subject")
    / @Html.ActionLink((ViewBag.Subject as Subject).Code, "Details", "Subject", new { id = (ViewBag.Subject as Subject).SubjectId }, null)
    /
    @{
        if (ViewBag.Section != null)
        {
        @Html.ActionLink("Sections", "Index", "Section", new { subjectId = (ViewBag.Subject as Subject).SubjectId }, null)
        <span>/</span>
        <span style="word-wrap: break-word">@Html.ActionLink((ViewBag.Section as Section).Name, "Index", "Question", new { subjectId = Model.SubjectId, sectionId = (ViewBag.Section != null ? ViewBag.Section.SectionId : 0) }, null)</span>
        <span>/</span>
        }
        else
        {
        @Html.ActionLink("Questions", "Index", "Question", new { subjectId = (ViewBag.Subject as Subject).SubjectId }, null)
        <span>/</span>
        }
    }
    Edit Question
</div>
<br />
<div class="main_nav">
    @if (@ViewBag.Section != null)
    {
        <span><a href="@Url.Content("~/Question/Details")?id=@Model.QuestionId&sectionId=@ViewBag.Section.SectionId">
            <img src="@Url.Content("~/Images/back button.png")" style="height: 30px"/>
        </a></span>
    }
    else
    {
        <span><a href="@Url.Content("~/Question/Index")?subjectId=@Model.SubjectId">
            <img src="@Url.Content("~/Images/back button.png")" style="height:30px"/>
        </a></span>
    }
</div>
<br />
<span class="require_info">(*) is required.</span>
@{
    using (Html.BeginForm())
    {
        <span class="errorMsg">@Html.ValidationSummary(true)</span>
        @Html.HiddenFor(model => model.LastEditedTime)
        @Html.HiddenFor(model => model.QuestionId)
        @Html.HiddenFor(model => model.QuestionTypeId)
        @Html.HiddenFor(model => model.SubjectId)
        <table class="table table-bordered">
            <tr>
                <td style="width: 150px; text-align: right">
                    <span>Question Type</span>
                </td>
                <td>
                    <span>@ViewBag.QuestionType</span>
                </td>
            </tr>
            <tr>
                <td style="width: 150px; text-align: right">
                    <span>Section</span>
                </td>
                <td>
                    @if (@ViewBag.Section != null)
                    {
                        <span>
                            @ViewBag.Section.Name
                        </span>
                    }
                    else
                    {
                        <span>
                            @Html.DropDownListFor(model => model.SectionId, ViewBag.SectionMineList as SelectList)
                        </span>
                    }
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    <span>Title (*)</span>
                </td>
                <td>
                    <span class="errorMsg">@Html.ValidationMessageFor(model => model.Title)</span>
                    @Html.TextBoxFor(model => model.Title, new {style = "width:400px"})
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    Question (*)
                </td>
                <td>
                    <span class="errorMsg">@Html.ValidationMessageFor(model => model.Content)</span>
                    @Html.TextAreaFor(model => model.Content, new {style = "width:97%", @class = "mceEditor"})
                </td>
            </tr>
            @{
                if (Model.QuestionTypeId == 1)
                {
                    for (int i = 0; i < Model.Answers.Count; i++)
                    {
                        <tr>
                            <td style="text-align: right">
                                Answer
                                @{
                                    int p = i + 1;
                                    @(p + " (*)")
                                    ;
                                }
                            </td>
                            <td>
                                <span class="errorMsg">@Html.ValidationMessageFor(model => model.Answers[i].Content)</span>
                                @Html.TextAreaFor(model => model.Answers[i].Content, new {style = "width:97%", @class = "mceEditor"})
                                <br />
                                @{
                                    var call = "check('class','" + i + "')";
                                }
                                @Html.CheckBoxFor(model => Model.Answers[i].Checked, new {type = "radio", @class = "class", id = i, onclick = @call})
                                <span>Is correct?</span>
                            </td>
                        </tr>
                    }
                    <tr>
                        <td>
                        </td>
                        <td>
                            <button type="submit" name="submit" value="More Answer" class="btn">
                                More Answer</button>
                            <button type="submit" value="Remove Last Answer" name="submit" class="btn">
                                Remove Last Answer</button>
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            <span>Explanation</span>
                        </td>
                        <td>
                            <span class="errorMsg">@Html.ValidationMessageFor(model => model.Explanation)</span>
                            @Html.TextAreaFor(model => Model.Explanation, new {style = "width:97%", @class = "mceEditor"})
                        </td>
                    </tr>
                }
                if (Model.QuestionTypeId == 2)
                {
                    for (int i = 0; i < Model.Answers.Count; i++)
                    {
                        <tr>
                            <td style="text-align: right">
                                <span>Answer</span>
                                @{
                                    int p = i + 1;
                                    @(p + " (*)")
                                    ;
                                }
                            </td>
                            <td>
                                <span class="errorMsg">@Html.ValidationMessageFor(model => model.Answers[i].Content)</span>
                                @Html.TextAreaFor(model => model.Answers[i].Content, new {style = "width:97%", @class = "mceEditor"})
                                <br />
                                <span class="errorMsg">@Html.ValidationMessageFor(model => model.Answers[i].Grade)</span>
                                <span>Grade: @Html.DropDownListFor(model => model.Answers[i].Grade, (IEnumerable<SelectListItem>) Session["AnswerList" + Model.Answers[i].AnswerId], new {style = "width:90px"})
                                    %</span>
                            </td>
                        </tr>
                    }
                    <tr>
                        <td>
                        </td>
                        <td>
                            <button type="submit" name="submit" value="More Answer" class="btn">
                                More Answer</button>
                            <button type="submit" value="Remove Last Answer" name="submit" class="btn">
                                Remove Last Answer</button>
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            <span>Explanation</span>
                        </td>
                        <td>
                            <span class="errorMsg">@Html.ValidationMessageFor(model => model.Explanation)</span>
                            @Html.TextAreaFor(model => model.Explanation, new {style = "width:97%", @class = "mceEditor"})
                        </td>
                    </tr>
                }
                if (Model.QuestionTypeId == 3)
                {
                    for (int i = 0; i < Model.Answers.Count; i++)
                    {
                        <tr>
                            <td style="text-align: right">
                                <span>Answer</span>
                                @{
                                    int p = i + 1;
                                    @(p + " (*)")
                                    ;
                                }
                            </td>
                            <td>
                                <span class="errorMsg">@Html.ValidationMessageFor(model => model.Answers[i].Content)</span>
                                @Html.TextAreaFor(model => model.Answers[i].Content, new {style = "width:97%", @class = "mceEditor"})
                                <br />
                                <span class="errorMsg">@Html.ValidationMessageFor(model => model.Matchs[i].Content)</span>
                                @Html.TextBoxFor(model => model.Matchs[i].Content, new {style = "width:400px", placeholder = "Match"})
                            </td>
                        </tr>
                    }
                    <tr>
                        <td>
                        </td>
                        <td>
                            <button type="submit" name="submit" value="More Answer" class="btn">
                                More Answer</button>
                            <button type="submit" value="Remove Last Answer" name="submit" class="btn">
                                Remove Last Answer</button>
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            <span>Other Invalid Matchs</span>
                        </td>
                        <td>
                            @{
                                if (Model.InvalidMatchs == null || Model.InvalidMatchs.Count == 0)
                                {
                                    <span>Empty.</span>
                                }
                                else
                                {
                                    for (var i = 0; i < Model.InvalidMatchs.Count; i++)
                                    {
                                        <span class="errorMsg">@Html.ValidationMessageFor(model => model.Matchs[i].Content)</span>
                                        @Html.TextBoxFor(model => model.InvalidMatchs[i].Content, new {style = "width:40%", placeholder = "Match"})
                                        <br />
                                    }
                                }
                            }
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <button type="submit" name="submit" value="More Match" class="btn">
                                More Match</button>
                            <button type="submit" value="Remove Last Match" name="submit" class="btn">
                                Remove Last Match</button>
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            <span>Explanation</span>
                        </td>
                        <td>
                            <span class="errorMsg">@Html.ValidationMessageFor(model => model.Explanation)</span>
                            @Html.TextAreaFor(model => model.Explanation, new {style = "width:97%", @class = "mceEditor"})
                        </td>
                    </tr>
                }
                if (Model.QuestionTypeId == 4)
                {
                    for (int i = 0; i < Model.Answers.Count; i++)
                    {
                        <tr>
                            <td style="text-align: right">
                                <span>Answer</span>
                                @{
                                    int p = i + 1;
                                    @p
                                    ;
                                }
                            </td>
                            <td>
                                @{

                                    var call = "check('class','" + i + "')";
                                }
                                @Html.CheckBoxFor(model => Model.Answers[i].Checked, new {type = "radio", @class = "class", id = i, onclick = @call})
                                @Html.DisplayFor(model => model.Answers[i].Content, new {style = "width:97%"})
                                @Html.HiddenFor(model => model.Answers[i].Content)
                            </td>
                        </tr>
                    }
                    <tr>
                        <td style="text-align: right">
                            <span>Explanation</span>
                        </td>
                        <td>
                            <span class="errorMsg">@Html.ValidationMessageFor(model => model.Explanation)</span>
                            @Html.TextAreaFor(model => model.Explanation, new {style = "width:97%", @class = "mceEditor"})
                        </td>
                    </tr>
                }
            }
            <tr>
                <td>
                </td>
                <td>
                    <button type="submit" value="Save" name="submit" class="btn" style="width: 80px">Save</button>
                    @if (@ViewBag.Section != null)
                    {
                        @Html.ActionLink("Reset", "Edit", new { id = @Model.QuestionId, sectionId = @ViewBag.Section.SectionId }, new { style = "width: 54px", @class = "btn" })
                        @Html.ActionLink("Cancel", "Details", new { id = @Model.QuestionId, sectionId = @ViewBag.Section.SectionId }, new { style = "width: 54px", @class = "btn" })
                    }
                    else
                    {
                        @Html.ActionLink("Reset", "Edit", "Question", new { id = @Model.QuestionId }, new { style = "width: 54px", @class = "btn" })
                        @Html.ActionLink("Cancel", "Details", new { id = @Model.QuestionId }, new { style = "width: 54px", @class = "btn" })
                    }
                </td>
            </tr>
        </table>
    }
}